import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '首页',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard', affix: true }
    }]
  },
  {
    path: '/shop',
    component: Layout,
    redirect: '/shop/shop-movie',
    name: '店内日常',
    meta: { title: '店内日常', icon: 'el-icon-s-help' },
    children: [{
      path: 'shop-movie',
      name: '店铺剧本',
      component: () => import('@/views/shop-movie/shop-movie'),
      meta: { title: '店铺剧本', icon: 'table' }
    }, {
      path: 'carpool',
      name: '拼车列表',
      component: () => import('@/views/carpool/carpools'),
      meta: { title: '拼车列表', icon: 'table' }
    }, {
      path: 'coupon-def',
      name: '优惠券定义',
      component: () => import('@/views/coupon/coupon-def'),
      meta: { title: '优惠券定义', icon: 'table' }
    }, {
      path: 'coupon',
      name: '优惠券发放',
      component: () => import('@/views/coupon/coupon'),
      meta: { title: '优惠券发放', icon: 'table' }
    }, {
      path: 'coupon-use-record',
      name: '优惠券使用',
      component: () => import('@/views/coupon/coupon-use-record'),
      meta: { title: '优惠券使用', icon: 'table' }
    }]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/org-units',
    name: '系统管理',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    children: [{
      path: 'movies',
      name: '剧本列表',
      component: () => import('@/views/movie/movie-list'),
      meta: { title: '剧本列表', icon: 'table' }
    }, {
      path: 'play-records',
      name: '开本记录',
      component: () => import('@/views/play-records/play-records'),
      meta: { title: '开本记录', icon: 'table' }
    }, {
      path: 'org-units',
      name: '店铺列表',
      component: () => import('@/views/org-units/index'),
      meta: { title: '店铺列表', icon: 'table' }
    }, {
      path: 'rooms',
      name: '房间列表',
      component: () => import('@/views/rooms/rooms'),
      meta: { title: '房间列表', icon: 'table' }
    }, {
      path: 'devices',
      name: '设备列表',
      component: () => import('@/views/devices/devices'),
      meta: { title: '设备列表', icon: 'table' }
    }, {
      path: 'users',
      name: '用户列表',
      component: () => import('@/views/users/users'),
      meta: { title: '用户列表', icon: 'table' }
    }, {
      path: 'franchisee',
      name: '加盟商列表',
      component: () => import('@/views/org-units/franchisee'),
      meta: { title: '加盟商列表', icon: 'table' }
    }, {
      path: 'charge-orders',
      name: '充值记录',
      component: () => import('@/views/charge-records/charge-records'),
      meta: { title: '充值记录', icon: 'table', roles: ['Admin', 'Manager', 'ShopManager'] }
    }, {
      path: 'return-list',
      name: '返利记录',
      component: () => import('@/views/return-money/return-money'),
      meta: { title: '返利记录', icon: 'table' }
    }, {
      path: 'movie-tests',
      name: '剧本内测',
      component: () => import('@/views/movie-inner-test/movie-inner-test'),
      meta: { title: '剧本内测', icon: 'table', roles: ['Admin'] }
    }]
  },
  {
    path: '/settings',
    component: Layout,
    redirect: '/settings/movie-tags.js',
    name: '参数设置',
    meta: { title: '参数设置', icon: 'el-icon-s-help' },
    children: [{
      path: 'movie-tags.js',
      name: '标签列表',
      component: () => import('@/views/tags/movie-tags'),
      meta: { title: '标签列表', icon: 'table' }
    }, {
      path: 'movie-levels',
      name: '剧本类别',
      component: () => import('@/views/movie-level/movie-level'),
      meta: { title: '剧本类别', icon: 'table' }
    }, {
      path: 'sys-params',
      name: '系统参数',
      component: () => import('@/views/sys-params/sys-params'),
      meta: { title: '系统参数', icon: 'form' }
    }, {
      path: 'holiday-config',
      name: '假期配置',
      component: () => import('@/views/sys-params/holiday-config'),
      meta: { title: '假期配置', icon: 'form', roles: ['Admin'] }
    }, {
      path: 'return-money-shop',
      name: '返利店铺',
      component: () => import('@/views/return-money/return-money-shop'),
      meta: { title: '返利店铺', icon: 'table', roles: ['Admin'] }
    }, {
      path: 'app-packages',
      name: 'App列表',
      component: () => import('@/views/app-package/app-packages'),
      meta: { title: 'App列表', icon: 'table', roles: ['Admin', 'Manager'], orgUnitType: 2 }
    }]
  },
  {
    path: '/reports',
    component: Layout,
    redirect: '/reports/consume-details',
    name: '报表',
    meta: { title: '报表', icon: 'el-icon-s-help', roles: ['Admin', 'Manager', 'ShopManager'] },
    children: [{
      path: 'consume-details',
      name: '消费明细报表',
      component: () => import('@/views/consume/consume-details'),
      meta: { title: '消费明细报表', icon: 'table', roles: ['Admin', 'Manager', 'ShopManager'] }
    }, {
      path: 'carpool-count-report',
      name: '发车量统计报表',
      component: () => import('@/views/carpool/carpool-count-report'),
      meta: { title: '发车量统计报表', icon: 'table', roles: ['Admin', 'Manager', 'ShopManager', 'CarpoolMaster', 'DM'] }
    }, {
      path: 'carpool-player-count-report',
      name: '推客量统计报表',
      component: () => import('@/views/carpool/carpool-player-count-report'),
      meta: { title: '推客量统计报表', icon: 'table', roles: ['Admin', 'Manager', 'ShopManager', 'CarpoolMaster', 'CustomerService'] }
    }]
  },
  {
    path: '/sys-operation',
    component: Layout,
    redirect: '/sys-operation/free-files',
    name: '系统运维',
    meta: { title: '系统运维', icon: 'el-icon-s-help', roles: ['Admin'] },
    children: [{
      path: 'free-files',
      name: '空闲文件',
      component: () => import('@/views/free-files/free-files'),
      meta: { title: '空闲文件', icon: 'table', roles: ['Admin'] }
    }, {
      path: 'movie-versions',
      name: '剧本版本列表',
      component: () => import('@/views/movie-versions/movie-versions'),
      meta: { title: '剧本版本列表', icon: 'table', roles: ['Admin'] }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
